﻿namespace BadHabit.WPFTools
{
    using System;

    public class DelayFilter
    {
        public delegate void ApplyFilterDelegate();

        private ApplyFilterDelegate applyFilterMethod;

        private System.Windows.Threading.DispatcherTimer searchTimer = new System.Windows.Threading.DispatcherTimer();

        public DelayFilter(int seconds, ApplyFilterDelegate applyFilterMethod)
        {
            this.applyFilterMethod = applyFilterMethod;
            this.searchTimer.Interval = new TimeSpan(0, 0, 1);
            this.searchTimer.Tick += new EventHandler(this.SearchTimerTick);
        }

        public void StartFilter()
        {
            if (!this.searchTimer.IsEnabled)
            {
                this.searchTimer.Start();
            }
        }

        private void SearchTimerTick(object sender, EventArgs e)
        {
            this.applyFilterMethod();
            this.searchTimer.Stop();
        }
    }
}
